<?php
	/*
	 	Handler Class for the Fileupload Module.
	*/
	
	class FileUploadHandler
	{
		static private $maxFileSize = 512000;
		static private $maxAllowedNumbErrors = 0;
		
		/* Gets files from a directory on the disk and returns them as an array */
		public function GetFilesFromDisk($dir)
		{
			// The array that will be returned
			$files = array();
			
			// If the directory exists
			if (is_dir($dir))
			{
				// Open the directory
		    	if ($dh = opendir($dir))
		    	{
		    		// Read the files from the directory
		        	while (($file = readdir($dh)) !== false)
		        		$files[] = $file;
		        	
		        	closedir($dh);
		    	}
			}
			
			return $files;
		}
		
		/* Uploads a file to the site */
		public function UploadFile($file)
		{
			// If there were any errors, return false to indicate it
			if ($_FILES[$file]["error"] > self::$maxAllowedNumbErrors)
				return false;
			
			// If the filesize is bigger then 5MB, return false
			if ($_FILES[$file]["size"] > self::$maxFileSize)
				return false;
			
			// If the file is already uploaded (might remove this)
			if (file_exists("Uploads/" . $_FILES[$file]["name"]))
				return false;
			
			// Put the uploaded file in the right directory
			move_uploaded_file($_FILES[$file]["tmp_name"], "Uploads/" . $_FILES[$file]["name"]);
			
			return true;
		}
		
		/* Deletes a file */
		public function DeleteFile($file)
		{
			unlink("Uploads/" . $file);
		}
		
		/* Automatic test of all functionality in this class */
		public static function Test()
		{
			$sut = new FileUploadHandler();
			$hasErrors = false;	
			
			return !$hasErrors;		
		}
	}
